package ch.vd.dec.selt.petale

import grails.converters.JSON

class GroupePrestationController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [groupePrestationInstanceList: GroupePrestation.list(params), groupePrestationInstanceTotal: GroupePrestation.count()]
    }

    def create = {
        def groupePrestationInstance = new GroupePrestation()
        groupePrestationInstance.properties = params
        return [groupePrestationInstance: groupePrestationInstance]
    }

    def save = {
        def groupePrestationInstance = new GroupePrestation(params)
        if (groupePrestationInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'groupePrestation.label', default: 'GroupePrestation'), groupePrestationInstance.id])}"
            redirect(action: "show", id: groupePrestationInstance.id)
        }
        else {
            render(view: "create", model: [groupePrestationInstance: groupePrestationInstance])
        }
    }

    def show = {
        def groupePrestationInstance = GroupePrestation.get(params.id)
        if (!groupePrestationInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'groupePrestation.label', default: 'GroupePrestation'), params.id])}"
            redirect(action: "list")
        }
        else {
            [groupePrestationInstance: groupePrestationInstance]
        }
    }

    def edit = {
        def groupePrestationInstance = GroupePrestation.get(params.id)
        if (!groupePrestationInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'groupePrestation.label', default: 'GroupePrestation'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [groupePrestationInstance: groupePrestationInstance]
        }
    }

    def update = {
        def groupePrestationInstance = GroupePrestation.get(params.id)
        if (groupePrestationInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (groupePrestationInstance.version > version) {
                    
                    groupePrestationInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'groupePrestation.label', default: 'GroupePrestation')] as Object[], "Another user has updated this GroupePrestation while you were editing")
                    render(view: "edit", model: [groupePrestationInstance: groupePrestationInstance])
                    return
                }
            }
            groupePrestationInstance.properties = params
            if (!groupePrestationInstance.hasErrors() && groupePrestationInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'groupePrestation.label', default: 'GroupePrestation'), groupePrestationInstance.id])}"
                redirect(action: "show", id: groupePrestationInstance.id)
            }
            else {
                render(view: "edit", model: [groupePrestationInstance: groupePrestationInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'groupePrestation.label', default: 'GroupePrestation'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def groupePrestationInstance = GroupePrestation.get(params.id)
        if (groupePrestationInstance) {
            try {
                groupePrestationInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'groupePrestation.label', default: 'GroupePrestation'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'groupePrestation.label', default: 'GroupePrestation'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'groupePrestation.label', default: 'GroupePrestation'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def getPrestation() {
		def entries = GroupePrestation.createCriteria().list {
				(params."grp.id" != null & params."grp.id"!="" & params."grp.id"!="null") ? groupe{eq('id', params."grp.id".toLong())} : true
				order("prestation", "asc")
			}
	}
	
	def ajaxGetPrestation = {
		def entries = getPrestation()
		
		render entries as JSON
	}
	
	def getGroupe() {
		def entries = GroupePrestation.createCriteria().list {
				(params."pre.id" != null & params."pre.id"!="" & params."pre.id"!="null") ? prestation{eq('id', params."pre.id".toLong())} : true
				order("groupe", "asc")
			}
	}
	
	def ajaxGetGroupe = {
		def entries = getGroupe()
		
		render entries as JSON
	}
}
